package com.xiang.producer;

import com.xiang.RabbitmqApplication;
import com.xiang.config.RabbitMqTopicConfig;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;

import static org.springframework.amqp.core.MessageProperties.CONTENT_TYPE_JSON;

/**
 * 生产者
 *
 * @author: xiangtianlei
 * @Date: 2022-9-2
 */
@SpringBootTest(classes = RabbitmqApplication.class)
@RunWith(SpringRunner.class)
public class ProducerTopicsTest {

    @Resource
    RabbitTemplate rabbitTemplate;

    @Test
    public void producerTopics() {
        // 消息体
        Message message = MessageBuilder.withBody("你好".getBytes()).build();
        // 设置类型
        message.getMessageProperties().setExpiration("100");
        // 设置类型
        message.getMessageProperties().setType(CONTENT_TYPE_JSON);

        /**
         *  1、交换机名称
         *  2、routingKey
         *  3、发送内容
         */
        rabbitTemplate.convertAndSend(RabbitMqTopicConfig.EXCHANGE_TOPICS_INFORM, "inform.xiang.email", message);
        rabbitTemplate.convertAndSend(RabbitMqTopicConfig.EXCHANGE_TOPICS_INFORM, "inform.xiang.sms", message);
    }

}
